package asapIs134CK.womi.client.games.sudoku;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class SudokuGame extends Composite{
	private int[][] _numbers;
	private boolean[][] _given;
	private AbsolutePanel panel;
	private List unknownTiles;
	private int score;
	public SudokuGame(int[][] numbers, boolean[][] given){
		score = 0;
		_numbers = numbers;
		_given = given;
		panel = new AbsolutePanel();
		unknownTiles = new ArrayList();
		initBoard();
		initWidget(panel);
	}
	public SudokuGame(SudokuGrid grid) {
		panel = new AbsolutePanel();
		unknownTiles = new ArrayList();
		initBoard(grid);
		initWidget(panel);
	}
	private void initBoard(SudokuGrid grid) {
		panel.setPixelSize(482,482);
		panel.setStylePrimaryName("Sudoku");
		// Add lines
		SimplePanel firstVerticalBar = new SimplePanel();
		firstVerticalBar.addStyleName("SudokuVerticalBar");
		panel.add(firstVerticalBar, 153,0);
		SimplePanel secondVerticalBar = new SimplePanel();
		secondVerticalBar.addStyleName("SudokuVerticalBar");
		panel.add(secondVerticalBar, 309,0);
		SimplePanel firstHorizontalBar = new SimplePanel();
		firstHorizontalBar.addStyleName("SudokuHorizontalBar");
		panel.add(firstHorizontalBar, 0, 153);
		SimplePanel secondHorizontalBar = new SimplePanel();
		secondHorizontalBar.addStyleName("SudokuHorizontalBar");
		panel.add(secondHorizontalBar, 0, 309);
		// Add tiles
		for(int i=0; i<9;i++){
			for(int j=0;j<9;j++){
				Widget tile;
				if(grid.isDefault(i, j)){
					tile = new SudokuGivenTile(grid.getVal(i, j));
				}
				else {
					tile = new SudokuUnknownTile(grid.getVal(i, j));
					unknownTiles.add(tile);
					score ++;
				}
				panel.add(tile, getXOffset(j), getYOffset(i));
			}
		}
	}
	private int getYOffset(int j) {
		return 52*j;
	}
	private int getXOffset(int i) {
		// TODO Auto-generated method stub
		return 52*i;
	}
	
	private void initBoard(){
		panel.setPixelSize(482,482);
		panel.setStylePrimaryName("Sudoku");
		// Add lines
		SimplePanel firstVerticalBar = new SimplePanel();
		firstVerticalBar.addStyleName("SudokuVerticalBar");
		panel.add(firstVerticalBar, 153,0);
		SimplePanel secondVerticalBar = new SimplePanel();
		secondVerticalBar.addStyleName("SudokuVerticalBar");
		panel.add(secondVerticalBar, 309,0);
		SimplePanel firstHorizontalBar = new SimplePanel();
		firstHorizontalBar.addStyleName("SudokuHorizontalBar");
		panel.add(firstHorizontalBar, 0, 153);
		SimplePanel secondHorizontalBar = new SimplePanel();
		secondHorizontalBar.addStyleName("SudokuHorizontalBar");
		panel.add(secondHorizontalBar, 0, 309);
		// Add tiles
		for(int i=0; i<9;i++){
			for(int j=0;j<9;j++){
				Widget tile;
				if(_given[i][j]){
					tile = new SudokuGivenTile(_numbers[i][j]);
				}
				else {
					tile = new SudokuUnknownTile(_numbers[i][j]);
					unknownTiles.add(tile);
				}
				panel.add(tile, getXOffset(j), getYOffset(i));
			}
		}
	}
	public boolean isCorrect(){
		boolean res = true;
		Iterator ite = unknownTiles.iterator();
		while(ite.hasNext()){
			SudokuUnknownTile tile = (SudokuUnknownTile)ite.next();
			if(!tile.isCorrect()){
				res = false;
				break;
			}
		}
		return res;
	}
	public void displayHint() {
		Iterator ite = unknownTiles.iterator();
		while(ite.hasNext()){
			SudokuUnknownTile tile = (SudokuUnknownTile)ite.next();
			if(!tile.isCorrect()){
				tile.displayHint();
				score--;
				break;
			}
		}
	}
	public int getScore() {
		// TODO Auto-generated method stub
		return score;
	}
}
